package test;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by yuanxin
 * Date:2019/6/18
 * Time:21:21
 */
public class binarySearchDemo {
    static Map<Integer, Integer> map = new HashMap<>();

    public static void main(String[] args) {
        int[] tab = new int[30];
        for (int i = 0; i < tab.length; i++) {
            tab[i] = i + 1;
        }
        for (int i = 0; i < tab.length; i++) {
            binarySearch(tab, tab[i]);
        }
        for (Map.Entry entry : map.entrySet()) {
            if (entry.getValue() == (Integer) 5) {
                System.out.println(entry.getKey() + ":" + entry.getValue());
            }
        }

    }

    public static Map<Integer, Integer> binarySearch(int[] tab, int key) {
        int low = 0;
        int high = tab.length - 1;
        int compTimes = 0;
        while (low <= high) {
            compTimes++;
            int mid = (low + high) / 2;
            if (tab[mid] == key) {
                map.put(key, compTimes);
                return map;
            } else if (tab[mid] > key) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return null;
    }
}
